我的对象很简单 - 我想要一个快捷方式,用大写字符替换选择中的文本,而不应用" AllCaps"风格,因为我不会进入这里。
我使用Selection.Delete
和Selection.Insert
取得了成功,但这似乎并不能保留段落样式不固有的字体样式。
当我尝试使用VBA Selection.Find.Execute:=ReplaceAll
时,它正在替换整个文档中的所有实例。我知道这应该是.wrap=wdFindContinue
的行为,但我设置了.wrap=wdFindStop
。我最好的猜测是,因为我的.text
被设置为调用Selection
对象的变量,所以在设置.text
时它会以某种方式丢失选择(尽管当我单步执行时看起来不明显代码选择保持突出显示,导致.ReplaceAll
执行整个文档。如果我手动输入"找到" (.text
)特定情况的字符串,它仅在选择上执行(即使在.Replacement.Text字段中调用变量txt
!),但显然不会实用。
这是我的代码:
Option Explicit
Sub ReplaceWithUppercase()
Dim pasteAutoFormatSetting As Boolean
Dim txt As String
'Save current user selection of whether to adjust spacing automatically when pasting:
pasteAutoFormatSetting = Options.PasteAdjustWordSpacing
'Turn off auto spacing adjustment:
Options.PasteAdjustWordSpacing = False
txt = Selection.Range.text
With Selection.Font
.AllCaps = False
End With
'The next two lines work but do not seem to preserve built-in font styles
'already applied to the selected text, so I wanted to instead use find/replace:
'Selection.Range.Delete
'Selection.Range.InsertAfter (UCase(txt))
With Selection.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.text = txt
.Replacement.text = UCase(txt)
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
'Return to user preference for auto apacing adjustment:
Options.PasteAdjustWordSpacing = pasteAutoFormatSetting
End Sub
答案 0 :(得分:1)
使用数组进行选择范围的快速工作 - 以及StrConv函数
Sub ChangeRangeToUCase()
Const vbUpperCase = 1 ' strConv constant
Dim arrCells As Variant
Dim intRow As Integer
Dim intCol As Integer
' Assign Range to an array
arrCells = Selection
For intRow = LBound(arrCells, 1) To UBound(arrCells, 1)
For intCol = LBound(arrCells, 2) To UBound(arrCells, 2)
arrCells(intRow, intCol) = StrConv(arrCells(intRow, intCol), vbUpperCase)
Next intCol
Next intRow
Selection = arrCells
End Sub
答案 1 :(得分:1)
@PatentWookiee,我认为你最初是在正确的轨道上,
Selection.Range.Delete
Selection.Range.InsertAfter (UCase(txt))
问题是,一旦删除了所选文本,放在其中的任何内容都将假定删除前第一个字符的格式。要解决这个问题,您需要使用:
Option Explicit
Sub ReplaceWithUppercase()
Selection.Text = UCase(Selection.Text)
End Sub
我们只需替换所选字符串的 case ,而不是替换实际文本(以及格式化),从而使格式完整无缺。如果我已经正确地阅读了您的问题,这应该可以完全解决您的问题。如果没有,请务必告诉我们,我们将通过它。
答案 2 :(得分:0)
在宏记录器的帮助下,我们发现:
private String id;
private int row_number;
//Fill first Combo
private void fill_first_combo()
{
datagridview_col1.Items.Clear();
if (datagridview.SelectedRows.Count > 0)
{
string rcs = db_conn.connection();
using (var OraConn = new OracleConnection(rcs))
{
using (var OraCmd = OraConn.CreateCommand())
{
try
{
OraConn.Open();
OraCmd.BindByName = true;
OraCmd.CommandText = "Oracle Command"
OracleDataReader OraDataReader = OraCmd.ExecuteReader();
if (OraDataReader.Read() == false)
{
//MessageBox
}
else
{
using (var OraDat = new OracleDataAdapter(OraCmd))
{
using (var table = new DataTable())
{
OraDat.Fill(table);
foreach (DataRow row in table.Rows)
{
foreach (DataColumn column in table.Columns)
{
if (row[column] != null)
{
if (column.ColumnName == "COLUMNNAME")
{
datagridview_col1.Items.Add(row[column.ColumnName].ToString());
id = row[column.ColumnName].ToString().Substring(0, 6);
}
}
}
}
}
}
}
}
catch (OracleException ex)
{
//Exceptions
}
finally
{
OraConn.Dispose();
}
}
}
}
else
{
//MessageBox
}
}
//Fill 2nd ComboBoxColumn
private void fill_combobox2(int row_number)
{
datagridview_col2.Items.Clear();
string rcs = db_conn.connection();
using (var OraConn = new OracleConnection(rcs))
{
using (var OraCmd = OraConn.CreateCommand())
{
try
{
OraConn.Open();
OraCmd.BindByName = true;
OraCmd.CommandText = "Oracle Command with id as a Parameter";
var id_param = new OracleParameter("id", id);
OraCmd.Parameters.Add(id_param);
OracleDataReader OraDataReader = OraCmd.ExecuteReader();
if (OraDataReader.Read() == false)
{
//MessageBox
}
else
{
using (var OraDat = new OracleDataAdapter(OraCmd))
{
using (var combo2_table = new DataTable())
{
OraDat.Fill(combo2_table);
foreach (DataRow row in combo2_table.Rows)
{
foreach (DataColumn column in combo2_table.Columns)
{
if (row[column] != null)
{
if (column.ColumnName == "BUILDING_LESS")
{
datagridview_col2.Items.Add(row[column.ColumnName].ToString());
}
}
}
}
}
}
}
}
catch (OracleException ex)
{
//Catch Exceptions
}
finally
{
OraConn.Dispose();
}
}
}
}
//Trigger SelectionChange
private void datagridview_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox cb = e.Control as ComboBox;
if (cb != null)
{
cb.SelectedIndexChanged -= new EventHandler(selectionchange);
cb.SelectedIndexChanged += selectionchange;
}
}
private void selectionchange(object sender, EventArgs e)
{
try
{
//ComboBox cb = (ComboBox)sender;
String selected = (sender as ComboBox).SelectedItem.ToString();
if (datagridview.CurrentCell.ColumnIndex == 0)
{
row_number = datagridview.CurrentCell.RowIndex;
id = selected.Substring(0, 6);
if (!String.IsNullOrEmpty(id))
{
fill_combobox2(row_number);
}
else if (String.IsNullOrEmpty(id))
{
//MessageBox);
}
}
else if (datagridview.CurrentCell.ColumnIndex == 1)
{
section = selected.Substring(0, 2);
if (!String.IsNullOrEmpty(id) && !String.IsNullOrEmpty(value2))
{
//MessageBox
}
else if (String.IsNullOrEmpty(id) && !String.IsNullOrEmpty(value2))
{
//MessageBox
}
else if (String.IsNullOrEmpty(id) && String.IsNullOrEmpty(value2))
{
//MessageBox
}
}
}
catch (Exception ex)
{
//MessageBox
}
}
可以接受吗?当然看起来容易得多。 OP担心不使用“ AllCapsStyle”。但是,使用注释掉的行,我们可以使自己确信,文档中的文本实际上已更改为大写,而不仅仅是其视觉外观(“样式”)。